import 'package:flutter/material.dart';
import './image.dart';

class PageViewSwiperPage extends StatefulWidget {
  const PageViewSwiperPage({super.key});

  @override
  State<PageViewSwiperPage> createState() => _PageViewSwiperPageState();
}

class _PageViewSwiperPageState extends State<PageViewSwiperPage> {
  List<Widget> list = [];
  int activeIndex = 0;
  @override
  void initState() {
    list.add(ImagePage(src: 'https://itying.com/images/flutter/1.png'));
    list.add(ImagePage(src: 'https://itying.com/images/flutter/2.png'));
    list.add(ImagePage(src: 'https://itying.com/images/flutter/3.png'));
    list.add(ImagePage(src: 'https://itying.com/images/flutter/4.png'));
    list.add(ImagePage(src: 'https://itying.com/images/flutter/5.png'));
    list.add(ImagePage(src: 'https://itying.com/images/flutter/6.png'));
    super.initState();
  }

  // 没有底部导航
  // @override
  // Widget build(BuildContext context) {
  //   return Scaffold(
  //       body: SizedBox(
  //     height: 300,
  //     width: double.infinity,
  //     child: PageView(
  //       children: list,
  //     ),
  //   ));
  // }

  // 会自动滑动
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SizedBox(
      height: 300,
      width: double.infinity,
      child: Stack(
        children: [
          PageView.builder(
              itemCount: 1000,
              onPageChanged: (index) {
                setState(() {
                  activeIndex = index % list.length;
                });
              },
              itemBuilder: (context, index) {
                return list[index % list.length];
              }),
          Positioned(
              left: 0,
              right: 0,
              bottom: 10,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: List.generate(6, (index) {
                  return Container(
                    margin: const EdgeInsets.all(5),
                    width: 10,
                    height: 10,
                    decoration: BoxDecoration(
                      color: activeIndex == index ? Colors.white : Colors.grey,
                      // 半圆
                      shape: BoxShape.circle,
                      // borderRadius: BorderRadius.circular(5)
                    ),
                  );
                }),
              ))
        ],
      ),
    ));
  }
}
